package com.hsjk.szwj.module.train.controller.app;

import com.hsjk.szwj.framework.common.pojo.CommonResult;
import com.hsjk.szwj.framework.common.pojo.PageResult;
import com.hsjk.szwj.framework.security.core.annotations.PreAuthenticated;
import com.hsjk.szwj.module.train.controller.app.vo.course.AppCourseArticleRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.course.AppCourseCatalogRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.course.AppCourseDetailRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.course.AppCourseItemRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.course.AppCourseLiveInfoRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.course.AppCoursePageReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.course.AppCoursePageRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.course.AppCourseStudyRecordReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.course.AppCourseTagRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.course.AppCourseVideoPointRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.course.AppCourseVideoRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.course.AppCourseWithPriceRespVO;
import com.hsjk.szwj.module.train.convert.AppCourseConvert;
import com.hsjk.szwj.module.train.dal.dataobject.course.CourseArticleDO;
import com.hsjk.szwj.module.train.dal.dataobject.course.CourseCatalogDO;
import com.hsjk.szwj.module.train.dal.dataobject.course.CourseTagDO;
import com.hsjk.szwj.module.train.dal.dataobject.course.CourseVideoDO;
import com.hsjk.szwj.module.train.dal.dataobject.course.CourseVideoPointDO;
import com.hsjk.szwj.module.train.service.ICourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.hsjk.szwj.framework.common.pojo.CommonResult.success;

/**
 * @author chenjiawei
 * @desc
 * @Date 2023/08/04 00:52
 **/
@Tag(name = "应用端 - 培训课程相关接口")
@RestController
@RequestMapping("/train/course")
@Validated
public class AppCourseController {

    @Resource
    private ICourseService courseService;

    @GetMapping("/list-tag")
    @Operation(summary = "获取课程标签列表")
    @PreAuthenticated
    public CommonResult<List<AppCourseTagRespVO>> getCourseTagList() {
        List<CourseTagDO> courseTagList = courseService.getCourseTagList();
        return success(AppCourseConvert.INSTANCE.convertAppCourseTagListResp(courseTagList));
    }

    @GetMapping("/get-catalog-list-by-course-id")
    @Operation(summary = "根据课程id获取课程目录")
    @PreAuthenticated
    public CommonResult<List<AppCourseCatalogRespVO>> getCourseCatalogListByCourseId(Long courseId) {
        List<CourseCatalogDO> listByCourseId = courseService.getCourseCatalogListByCourseId(courseId);
        return success(AppCourseConvert.INSTANCE.convertAppCourseCatalogListRespVO(listByCourseId));
    }

    @PostMapping("/get-course-page")
    @Operation(summary = "获取课程分页列表")
    @PreAuthenticated
    public CommonResult<PageResult<AppCourseWithPriceRespVO>> getCoursePage(
            @RequestBody @Valid AppCoursePageReqVO reqVO
    ) {
        if (StringUtils.isNotEmpty(reqVO.getCarType()) && reqVO.getCarType().equals("全部")) {
            reqVO.setCarType("");
        }
        PageResult<AppCourseWithPriceRespVO> coursePage = courseService.getCoursePage(reqVO);
        return success(coursePage);
    }

    @PostMapping("/get-purchased-course-page")
    @Operation(summary = "获取已购买课程分页列表")
    @PreAuthenticated
    public CommonResult<PageResult<AppCoursePageRespVO>> getPurchasedCoursePage(
            @RequestBody @Valid AppCoursePageReqVO reqVO
    ) {
        return success(courseService.getPurchasedCoursePage(reqVO));
    }

    @GetMapping("/get-course-by-id")
    @Operation(summary = "根据课程id获取课程详情")
    @PreAuthenticated
    public CommonResult<AppCourseDetailRespVO> getCourseById(Long id) {
        AppCourseDetailRespVO courseById = courseService.getCourseDetailById(id);
        return success(courseById);
    }

    @GetMapping("/get-item-list-by-course-id")
    @Operation(summary = "根据课程id获取课程条目")
    @PreAuthenticated
    public CommonResult<List<AppCourseItemRespVO>> getCourseItemListByCourseId(Long courseId) {
        return success(courseService.getCourseItemListByCourseId(courseId));
    }

    @GetMapping("/get-video-by-id")
    @Operation(summary = "根据课程id获取课程视频")
    @PreAuthenticated
    public CommonResult<AppCourseVideoRespVO> getCourseVideoById(Long id) {
        CourseVideoDO courseVideoById = courseService.getCourseVideoById(id);
        return success(AppCourseConvert.INSTANCE.convertAppCourseVideoListRespVO(courseVideoById));
    }

    @GetMapping("/get-article-by-id")
    @Operation(summary = "根据课程id获取课程文章")
    @PreAuthenticated
    public CommonResult<AppCourseArticleRespVO> getArticleById(Long id) {
        CourseArticleDO articleById = courseService.getArticleById(id);
        return success(AppCourseConvert.INSTANCE.convertAppCourseArticleListRespVO(articleById));
    }

    @GetMapping("/get-live-info-by-live-id")
    @Operation(summary = "根据直播id获取直播信息")
    @PreAuthenticated
    public CommonResult<AppCourseLiveInfoRespVO> getLiveInfoByLiveId(Long liveId) {
        AppCourseLiveInfoRespVO liveInfoByLiveId = courseService.getLiveInfoByLiveId(liveId);
        return success(liveInfoByLiveId);
    }

    @PostMapping("/update-study-record")
    @Operation(summary = "更新学习进度")
    @PreAuthenticated
    public CommonResult<Boolean> updateStudyRecord(@RequestBody @Valid AppCourseStudyRecordReqVO reqVO) {
        return success(courseService.updateStudyRecord(AppCourseConvert.INSTANCE.convert(reqVO)));
    }

    @GetMapping("/get-video-point-by-video-id")
    @Operation(summary = "根据视频id获取视频分段")
    @PreAuthenticated
    public CommonResult<List<AppCourseVideoPointRespVO>> getVideoPointByVideoId(Long videoId) {
        List<CourseVideoPointDO> videoPointByVideoId = courseService.getVideoPointByVideoId(videoId);
        return success(AppCourseConvert.INSTANCE.convert(videoPointByVideoId));
    }

    @GetMapping("/get-player-token-by-video-id")
    @Operation(summary = "根据视频id获取播放token")
    @PreAuthenticated
    public CommonResult<String> getPlayerTokenByVideoId(Long videoId) {
        return success(courseService.getPlayerTokenByVideoId(videoId));
    }

    @PostMapping("/list-item")
    @Operation(summary = "获取课程目录下item集合")
    @PreAuthenticated
    public CommonResult<List<Object>> getCatalogItemList(@RequestParam Long courseCatalogId) {
        return success(courseService.getCatalogItemList(courseCatalogId));
    }
}
